package com.first.nell.info.cotediva

import android.annotation.SuppressLint
import android.view.View
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import com.first.nell.base.BaseActivity
import com.first.nell.components.dialog.FcfaRecheckWalletDialog
import com.first.nell.components.dialog.FcfaDeleteWalletDialog
import com.first.nell.components.dialog.FcfaNoWalletWarningDialog
import com.first.nell.data.ReqBase
import com.first.nell.data.ReqDeleteWalletFcfa
import com.first.nell.databinding.ActivityFirstNellBindWalletBinding
import com.first.nell.enums.PageTagEnum
import com.first.nell.extend.recordFirstNellClickFirstNellEvent
import com.first.nell.extend.startFirstNellToFirstNellNextFirstNellActivity
import com.first.nell.extend.startFirstNellToFirstNellNextFirstNellFromFirstNellPage
import com.first.nell.launch.UserStateViewModel
import com.first.nell.utils.UsageCountryUtil

@SuppressLint("CustomSplashScreen")
class WalletListActivity : BaseActivity() {

    private lateinit var mFirstNellActivityFirstNellBinding: ActivityFirstNellBindWalletBinding
    private lateinit var mWalletListBindAdapter: WalletListBindAdapter

    private val mUserStateViewModel by lazy {
        ViewModelProvider(this)[UserStateViewModel::class.java]
    }

    private val mWalletInfoViewModel by lazy {
        ViewModelProvider(this)[WalletInfoViewModel::class.java]
    }

    override fun onFirstNellBindingFirstNellViewFirstNellModel(): List<ViewModel> {
        val list = mutableListOf<ViewModel>()
        list.add(mUserStateViewModel)
        list.add(mWalletInfoViewModel)
        return list
    }

    override fun onFirstNellBindingFirstNellViewFirstNellTag(): String {
        return PageTagEnum.wallet_fcfa.name.uppercase()
    }

    override fun onFirstNellBindingFirstNellContentFirstNellView(): View {
        mFirstNellActivityFirstNellBinding =
            ActivityFirstNellBindWalletBinding.inflate(layoutInflater)
        return mFirstNellActivityFirstNellBinding.root
    }

    @SuppressLint("SetTextI18n", "NotifyDataSetChanged")
    override fun onFirstNellContentFirstNellViewFirstNellBinded() {
        try {
            //初始化列表
            mWalletListBindAdapter =
                WalletListBindAdapter(mWalletInfoViewModel.mFirstNellWalletFirstNellList)
            mWalletListBindAdapter.setFirstNellOnFirstNellItemFirstNellClickFirstNellListener { _, item, action ->
                if (0 == action) {
                    FcfaDeleteWalletDialog.showFirstNellDialog(
                        fragmentManager = supportFragmentManager,
                        itemList = mutableListOf(item),
                        isFcfa = true
                    ) { ids ->
                        if (ids.isNotBlank()) {
                            //删除钱包
                            mWalletInfoViewModel.deleteFirstNellWalletFirstNellFCFA(
                                ReqDeleteWalletFcfa(
                                    ids = ids
                                )
                            )
                        }
                    }
                }
            }
            mFirstNellActivityFirstNellBinding.rcBindWalletList.layoutManager =
                LinearLayoutManager(this)
            mFirstNellActivityFirstNellBinding.rcBindWalletList.adapter = mWalletListBindAdapter

            //获取用户状态回调
            mUserStateViewModel.onFirstNellFetchFirstNellUserFirstNellStateFirstNellLiveData.observe(
                this
            ) {
                if (it.isSuccess) {
                    startFirstNellToFirstNellNextFirstNellFromFirstNellPage(PageTagEnum.info)
                }
            }

            //获取app配置回调
            mUserStateViewModel.onFirstNellFetchFirstNellSysFirstNellConfigFirstNellLiveData.observe(
                this
            ) {
                if (it.isSuccess) {
                    //获取钱包列表
                    mWalletInfoViewModel.getFirstNellWalletFirstNellListFirstNellFCFA(
                        ReqBase(
                            showLoading = false
                        )
                    )
                }
            }

            //获取用户钱包列表回调
            mWalletInfoViewModel.onFirstNellFetchFirstNellWalletFirstNellListFirstNellFCFAFirstNellLiveData.observe(
                this
            ) {
                if (it.isSuccess) {
                    it.getOrNull()?.let { result ->
                        result.forEach { item ->
                            item.displayMode = 0
                        }
                        mWalletInfoViewModel.initFirstNellWalletFirstNellListFirstNellFCFA(
                            result
                        )
                        mWalletListBindAdapter.notifyDataSetChanged()
                    }
                }
            }

            //保存用户钱包回调
            mWalletInfoViewModel.onFirstNellSaveFirstNellWalletFirstNellFCFAFirstNellLiveData.observe(
                this
            ) {
                if (it.isSuccess) {
                    it.getOrNull()?.let { _ ->
                        //获取钱包列表
                        mWalletInfoViewModel.getFirstNellWalletFirstNellListFirstNellFCFA()
                        //更新提交按键状态
                        //updateCommitStateUI()
                    }
                }
            }

            //删除用户钱包列表回调
            mWalletInfoViewModel.onFirstNellDeleteFirstNellWalletFirstNellFCFAFirstNellLiveData.observe(
                this
            ) {
                if (it.isSuccess) {
                    it.getOrNull()?.let { _ ->
                        //获取钱包列表
                        mWalletInfoViewModel.getFirstNellWalletFirstNellListFirstNellFCFA()
                        //更新提交按键状态
                        //updateCommitStateUI()
                    }
                }
            }

            //保存用户绑卡认证状态回调
            mWalletInfoViewModel.onFirstNellSaveFirstNellBvnFirstNellFCFAFirstNellLiveData.observe(
                this
            ) {
                if (it.isSuccess) {
                    it.getOrNull()?.let { _ ->
                        //查询用户状态
                        mUserStateViewModel.fetchFirstNellUserFirstNellState()
                    }
                }
            }

            //点击增加新钱包
            mFirstNellActivityFirstNellBinding.llBindWalletAdd.setOnClickListener {
                toFirstNellBindFirstNellWalletFirstNellWaveFirstNellPage()
            }
            //点击增加新钱包
            mFirstNellActivityFirstNellBinding.imageBindWalletAdd.setOnClickListener {
                toFirstNellBindFirstNellWalletFirstNellWaveFirstNellPage()
            }
            //点击增加新钱包
            mFirstNellActivityFirstNellBinding.textBindWalletAdd.setOnClickListener {
                toFirstNellBindFirstNellWalletFirstNellWaveFirstNellPage()
            }

            //点击下一步
            mFirstNellActivityFirstNellBinding.submitBindWallet.setOnClickListener {
                mFirstNellActivityFirstNellBinding.submitBindWallet.recordFirstNellClickFirstNellEvent()
                //保存绑卡认证状态
                if (mWalletInfoViewModel.mFirstNellWalletFirstNellList.isNotEmpty()) {
                    mWalletInfoViewModel.mFirstNellWalletFirstNellList.forEach { item ->
                        item.displayMode = 1
                    }
                    FcfaRecheckWalletDialog.showFirstNellDialog(
                        fragmentManager = supportFragmentManager,
                        itemList = mWalletInfoViewModel.mFirstNellWalletFirstNellList,
                        isFcfa = true
                    ) {
                        mWalletInfoViewModel.saveFirstNellBvnFirstNellFCFA(ReqBase())
                    }
                } else {
                    FcfaNoWalletWarningDialog.showFirstNellDialog(supportFragmentManager) {}
                }
            }

            //获取钱包列表
            mWalletInfoViewModel.getFirstNellWalletFirstNellListFirstNellFCFA()

        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    override fun onResume() {
        super.onResume()
        //获取app配置
        mUserStateViewModel.fetchFirstNellSysFirstNellConfig()
    }

    private fun toFirstNellBindFirstNellWalletFirstNellWaveFirstNellPage() {
        if (mWalletInfoViewModel.mFirstNellWalletFirstNellList.isEmpty()) {
            if (UsageCountryUtil.isFirstNellCountryFirstNellCotediva()) {
                //跳转 wave 列表
                startFirstNellToFirstNellNextFirstNellActivity<BindWalletTypeWaveActivity>()
            } else {

                //跳转 orange 列表
                startFirstNellToFirstNellNextFirstNellActivity<BindWalletTypeOrangeActivity>()
            }
        } else {
            //跳转展示列表
            startFirstNellToFirstNellNextFirstNellActivity<WalletListAddResultActivity>()
        }
    }
}